<?php

use Yaf\Session;
use Yaf\Registry;
use models\Business\AlibabaCategory;
use models\Business\Alibaba;
use Task\client;


class AlibabacategoryController extends ApiBaseController{

    /**
     * @SWG\Post(
     *     path="/admin/alibabacategory/create",
     *     tags={"1688商品分类"},
     *     summary="创建1688商品分类接口",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="1688商品分类编号",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="name",
     *          description="商品分类名称",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="listorder",
     *          description="排序字段",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="parent",
     *          description="上级分类",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="publishing",
     *          description="是否允许发布商品(0为否,1为是,默认0)",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="min_order_quantity",
     *          description="最小起订量",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="stats",
     *          description="状态(0为下架,1为上架,默认1)",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="201",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/alibabacategorySingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="创建失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function createAction() {
        $parameters = Registry::get('parameters');
        return $this->_responseJson(['data' => $parameters]);
    }


    /**
     * @SWG\Get(
     *     path="/admin/alibabacategory/list",
     *     tags={"1688商品分类"},
     *     summary="获取1688商品分类列表数据接口",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page",
     *          description="页码",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page_size",
     *          description="每页显示条数",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sort",
     *          description="排序方式 可选值: desc|asc",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="order",
     *          description="排序字段 可选值: id",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="name",
     *          description="商品分类名称",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="stats",
     *          description="状态 1为上架、0为下架",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="分类编号",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/alibabacategoryListData")
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function listAction() {
        $parameters = Registry::get('parameters');

        $alibaba_category_list = AlibabaCategory::getAlibabaCategoryList($parameters);

        return $this->_responseJson($alibaba_category_list);
    }


    /**
     * @SWG\Put(
     *     path="/admin/alibabacategory/{id}/edit",
     *     tags={"1688商品分类"},
     *     summary="编辑1688商品分类接口",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="/admin/Alibabacategory/list接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="1688商品分类编号",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="name",
     *          description="商品分类名称",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="listorder",
     *          description="排序字段",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="parent",
     *          description="上级分类",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="publishing",
     *          description="是否允许发布商品(0为否,1为是,默认0)",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="min_order_quantity",
     *          description="最小起订量",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="stats",
     *          description="状态(0为下架,1为上架,默认1)",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/alibabacategorySingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function editAction() {
        $parameters = Registry::get('parameters');

        $alibaba_category = AlibabaCategory::editAlibabaCategory($parameters);

        return $this->_responseJson(['data' => $parameters]);
    }


    /**
     * @SWG\DELETE(
     *     path="/admin/alibabacategory/{id}/delete",
     *     tags={"1688商品分类"},
     *     summary="删除1688商品分类接口",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="/admin/Alibabacategory/list接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="删除成功, 没有返回结果"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function deleteAction() {
        $parameters = Registry::get('parameters');
        return $this->_responseJson(['data' => $parameters]);
    }

    /**
     * @SWG\Get(
     *     path="/admin/alibaba/cai",
     *     tags={"1688商品分类"},
     *     summary="获取1688选品库数据",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page",
     *          description="页码",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page_size",
     *          description="每页显示条数",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/alibabacategoryListData")
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function groupAction() {
        try {
            $parameters = Registry::get('parameters');
            Registry::get('alibaba_log')->info(__METHOD__ . ' parameters: ' . json_encode($parameters));

            $parameters['page_size'] = 20;

            $user_group = Alibaba::getProductListCybUserGroup($parameters['page'], $parameters['page_size']);

            if (empty($user_group) && (! isset($user_group['result']['result']) || empty($user_group['result']['result'])))
                throw new Exception("选品库数据获取失败", 400);

            $key = 'alibaba_user_group';
            $alibaba_user_group_list = Registry::get('redis_list')->getList($key);
            if (! empty($alibaba_user_group_list))
                Registry::get('redis_list')->deleteList($key);

            Registry::get('redis_list')->genList($key, $user_group['result']['result']);

            return $this->_responseJson(['data' => $user_group['result']['result']]);
        } catch (Exception $e) {
            Registry::get('alibaba_log')->err(__METHOD__ . ' error: ' . $e->getMessage());
            throw $e;
        }
    }

    /**
     * @SWG\Put(
     *     path="/admin/alibaba/{id}/product",
     *     tags={"1688商品分类"},
     *     summary="获取指定1688选品库下的商品数据",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="id",
     *          description="/admin/alibaba/cai接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="timestamp",
     *          description="当前时间戳",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/alibabacategorySingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function productAction() {
        set_time_limit(300);
        $parameters = Registry::get('parameters');

        $key = 'alibaba_user_group';
        $user_group = Registry::get('redis_list')->getList($key);
        if (empty($user_group))
            throw new Exception("选品库不存在", 400);

        try {

            $page_size = 20;

            foreach ($user_group as $group_info) {
                if ($group_info['id'] == $parameters['id']) {
                    $total_page = ceil($group_info['feedCount'] / $page_size);
                    Registry::get('user_log')->info(__METHOD__ . ' total_page: ' . $page_size);
//                    Registry::get('user_log')->info(__METHOD__ . ' methods: ' . json_encode(get_class_methods($client)));
                    for ($page = 1; $page <= $total_page; $page++) {
                        $client = new client();
                        $client->connect();
                        Registry::get('user_log')->info(__METHOD__ . ' page: ' . $page);
                        Registry::get('user_log')->info(__METHOD__ . ' data: ' . json_encode([$group_info['id'], $page, $page_size]));
                        $client->productListCybUserGroupFeed($group_info['id'], $page, $page_size);
                        $client->close();
                        sleep(0.5);
                    }
                }
            }
        } catch (Exception $e) {
            Registry::get('user_log')->err('后台采集商品失败:' . $e->getMessage());
        } finally {
        }

        return $this->_responseJson(new stdClass(), '000', '商品库已经开始采集');
    }
}
